package com.zq.it.dao.impl.admin;

import java.util.Collection;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zq.it.dao.admin.TClassDao;
import com.zq.it.dao.impl.BaseDao;
import com.zq.it.pojo.TClass;

public class TClassDaoImpl extends BaseDao<TClass> implements TClassDao {
	/* public static final Logger logger=t(TClassDaoImpl.tclass); */
	@Override
	public void saveClassEty(TClass academy) {
		getHibernateTemplate().save(academy);
	}
	@Override
	public void deleteClassEtyByID(Integer id) {
		deleteEntity("delete TClass as c where c.classId=" + id);
	}

	@Override
	public List<TClass> findClassEtyList() {
		// final List list;
		// return getHibernateTemplate().executeFind(new HibernateCallback(){
		// @Override
		// public List<TClass> doInHibernate(final Session session) /*throws
		// HibernateException,SQLException*/
		// {
		// //org.hibernate.Transaction t=session.beginTransaction();
		// return
		// session.createCriteria(TClass.class).setFetchMode("major",org.hibernate.FetchMode.JOIN).list();
		//// t.commit();
		//// return list;
		// }
		// });
		return getHibernateTemplate().find("from TClass");
	}

	@Override
	public List findClassEtyById(Integer id) {
		return getHibernateTemplate()
				.find("select classId from TClass where classId=?", id);
		// TODO Auto-generated method stub

	}

	@Override
	public List findClassEtyByName(String name) {
		return getHibernateTemplate()
				.find("from TClass as c where c.className=?", name);
		// TODO Auto-generated method stub

	}

	@Override
	public void updateClassEty(TClass tclass) {
		getHibernateTemplate().update(tclass);
		// TODO Auto-generated method stub

	}
	// @Override
	// public List findClassEtyByDate(Date date){
	// // TODO Auto-generated method stub
	// return getHibernateTemplate().find("from TClass where
	// createTime=?",date);
	// }
	@Override
	public List findClassEtyByExmp(TClass tclass) {
		// TODO Auto-generated method stub
		return getHibernateTemplate().findByExample(tclass);
	}
	@Override
	public void deleteAllEty(int[] ids) {

		org.springframework.orm.hibernate3.HibernateTemplate ht = getHibernateTemplate();
		for (int i : ids)
			ht.delete(ht.load(com.zq.it.pojo.TClass.class, i));
	}
	@Override
	public void deleteClassEtyByObj(TClass tclass) {
		getHibernateTemplate().delete(tclass);
	}
	@Override
	public void saveClassEtyList(Collection<TClass> collection) {
		getHibernateTemplate().saveOrUpdateAll(collection);
	}
	@Override
	public List findClassEtyBySQL(String hql) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().find(hql);
	}
	@Override
	public List findClassNameAndId() {
		// TODO Auto-generated method stub
		return getHibernateTemplate()
				.find("select classId,className from TClass");
	}
	@Override
	public List findClassByHQL(String hql) {
		// TODO Auto-generated method stub
		return findEtyByHql(hql);
	}

	private static final Logger log = LoggerFactory
			.getLogger(TClassDaoImpl.class);
	// property constants
	public static final String CLASS_NAME = "className";
	public static final String ISHAVE_TASK = "ishaveTask";
	public static final String ENTEND_FIELD = "entendField";

	@Override
	public void save(TClass transientInstance) {
		log.debug("saving TClass instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	@Override
	public void delete(TClass persistentInstance) {
		log.debug("deleting TClass instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	@Override
	public TClass findById(Integer id) {
		log.debug("getting TClass instance with id: " + id);
		try {
			TClass instance = (TClass) getHibernateTemplate()
					.get("com.zq.it.pojo.TClass", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	@Override
	public List findByExample(TClass instance) {
		log.debug("finding TClass instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TClass instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from TClass as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	@Override
	public List findByClassName(Object className) {
		return findByProperty(CLASS_NAME, className);
	}

	@Override
	public List findByIshaveTask(Object ishaveTask) {
		return findByProperty(ISHAVE_TASK, ishaveTask);
	}

	@Override
	public List findByEntendField(Object entendField) {
		return findByProperty(ENTEND_FIELD, entendField);
	}

	@Override
	public List findAll() {
		log.debug("finding all TClass instances");
		try {
			String queryString = "from TClass";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	@Override
	public void update(TClass instance) {
		log.debug("attaching dirty TClass instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	@Override
	public List findByHql(String hql) {
		log.debug("finding TClass instances by Hql");
		try {
			return getHibernateTemplate().find(hql);
		} catch (RuntimeException re) {
			log.error("find by Hql failed", re);
			throw re;
		}
	}

	@Override
	public void deleteByHql(String hql) {
		super.deleteEntity(hql);
	}
}
